<?php

namespace App\Http\Controllers\Admin\Permission;

use App\Http\Controllers\Admin\BaseController;
use App\Repository\PermissionRepository;
use Illuminate\Http\Request;

use App\Http\Requests;

class PermissionController extends BaseController
{

    protected $permission;

    public function __construct(PermissionRepository $permission)
    {
        $this->middleware('permission:permission_manage');
        $this->permission = $permission;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $permissions = $this->permission->all();

        return view('admin.permission.permission', ['permissions' => $permissions]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request, [
            'name' => 'required'
        ], [
            'name.required' => '必须填写权限名'
        ]);

        $permission = $request->all();

        $displayName = isset($permission['display_name']) && $permission['display_name'] ? $permission['display_name'] : $permission['name'];

        $this->permission->add($permission['name'], $displayName , $permission['description']);

        return $this->success('增加权限成功');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $this->permission->delete($id);

        return $this->success('删除权限成功', url('admin/permission/permission'));
    }
}
